package com.hengda.smart.speak.d.db;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

/**
 *
 * @Description
 *  1 getAllResoure 获取数据库整个资源
 *  2 getInfoByAutoNum 根据自动号获取数据库信息
 * @author wzq
 * @date 2015-7-16 上午11:10:52
 * @update (date)
 * @version V1.0
 */

public class ResourceDb {
    private final static String FIELD_PLAYNUMS = "AutoNum";
    private final static String FIELD_FILENO = "FileNo";
    private final static String FIELD_TYPENUM = "UnitType";
    private String CurDBFileName;
    private SQLiteDatabase db = null;
    private Cursor c = null;
    private volatile static ResourceDb _instance;
    private ResourceDb() {
    }
    public static ResourceDb get_instance() {
        if (_instance == null)
            synchronized (ResourceDb.class) {
                if (_instance == null) {
                    _instance = new ResourceDb();
                }
            }
        return _instance;
    }


    public boolean OpenDB(String DBPathFile) {
//        KJLoger.debug(DBPathFile);
        CurDBFileName = DBPathFile;
        if (db != null) {
            db.close();
        }
        if (c != null) {
            c.close();
        }
        try {
            db = SQLiteDatabase.openDatabase(DBPathFile, null,
                    SQLiteDatabase.OPEN_READWRITE
                            | SQLiteDatabase.NO_LOCALIZED_COLLATORS);
            return true;
        } catch (Exception E) {
            db = null;
            return false;
        }
    }


    public void CloseDB() {
        if (db != null) {
            db.close();
            db = null;
        }
        if (c != null) {
            c.close();
            c = null;
        }
    }

    public int GetLangTotal() {
        int TempInt = 0;
        if (db == null) {
            try {
                db = SQLiteDatabase.openDatabase(CurDBFileName, null,
                        SQLiteDatabase.OPEN_READWRITE
                                | SQLiteDatabase.NO_LOCALIZED_COLLATORS);
            } catch (Exception E) {
                db = null;
            }
        }

        if (db != null) {
            if (c != null) {
                c.close();
                c = null;
            }
            try {
                c = db.rawQuery(
                        "SELECT * FROM sqlite_master where name <> 'android_metadata' and name <> 'sqlite_sequence'",
                        null);
            } catch (Exception E) {
                c = null;
            }
            if (c != null) {
                TempInt = c.getCount();
                c.close();
                c = null;
            }
        }
        return TempInt;
    }

    public String GetNextLang(String CurLang) {
        String Temp = null;
        if (db == null) {
            try {
                db = SQLiteDatabase.openDatabase(CurDBFileName, null,
                        SQLiteDatabase.OPEN_READWRITE
                                | SQLiteDatabase.NO_LOCALIZED_COLLATORS);
            } catch (Exception E) {
                db = null;
            }
        }
        if (db != null) {
            if (c != null) {
                c.close();
                c = null;
            }
            try {
                c = db.rawQuery(
                        "SELECT * FROM sqlite_master where name <> 'android_metadata' and name <> 'sqlite_sequence'",
                        null);
            } catch (Exception E) {
                c = null;
            }
            if (c != null && c.getCount() > 0) {
                if (CurLang == null) {
                    c.moveToFirst();
                    Temp = c.getString(2);
                } else {
                    c.moveToFirst();
                    if (c.getString(2).equals(CurLang)) {
                        if (c.moveToNext()) {
                            Temp = c.getString(2);
                        } else {
                            c.moveToFirst();
                            Temp = c.getString(2);
                        }
                    } else {
                        while (c.moveToNext()) {
                            if (c.getString(2).equals(CurLang)) {
                                if (c.moveToNext()) {
                                    Temp = c.getString(2);
                                } else {
                                    c.moveToFirst();
                                    Temp = c.getString(2);
                                }
                                break;
                            }
                        }
                    }
                }
            }
        }
        if (c != null) {
            c.close();
            c = null;
        }
        return Temp;
    }

    /**
     * @Description: 获取存放万片的资源
     * @param tableName
     * @return
     * @return Cursor
     * @throws
     * @autour wzq
     * @date 2015-11-19 上午10:23:32
     * @update (date)
     */
    public Cursor getMapInfoList(String tableName) {
        return Query(tableName, "SELECT  * FROM " + tableName);
    }


    /***
     * @Description: 根据MAPID获取资源详情
     * @param tableName
     * @param mapId
     * @return
     * @return Cursor
     * @throws
     * @autour wzq
     * @date 2015-11-19 上午10:25:50
     * @update (date)
     */
    public Cursor getMapInfo(String tableName, String mapId){
        return Query(tableName, "SELECT  * FROM " + tableName+" WHERE mapId = "+mapId);
    }

    /**
     * @Description: 获取分类集合，展厅列表
     * @param CurLang
     * @return
     * @return Cursor
     * @throws
     * @autour WZQ
     * @date 2016-6-21 上午8:47:29
     * @update (date)
     */
    public Cursor getUnitList(String CurLang) {
        if (db == null) {
            try {
                db = SQLiteDatabase.openDatabase(CurDBFileName, null,
                        SQLiteDatabase.OPEN_READWRITE
                                | SQLiteDatabase.NO_LOCALIZED_COLLATORS);
            } catch (Exception E) {
                db = null;
            }
        }
        if (db != null) {
            if (c != null) {
                c.close();
                c = null;
            }
            try {
                c = db.rawQuery("SELECT * FROM " + CurLang.trim(), null);
//                KJLoger.debug("=开始从库里面读="+"SELECT * FROM " + CurLang+c.getCount());
            } catch (Exception E) {
                c = null;
            }
        }
        if (c != null) {
            if (c.getCount() <= 0) {
                c.close();
                c = null;
            }
        }
        return c;
    }

    /**
     * @Description: 更具分类获取资源详情
     * @return
     * @return Cursor
     * @throws
     * @autour WZQ
     * @date 2016-6-21 上午8:48:11
     * @update (date)
     */
    public Cursor getRsInfoListByUnit(String tableName, int unitNo) {
        if (db == null) {
            try {
                db = SQLiteDatabase.openDatabase(CurDBFileName, null,
                        SQLiteDatabase.OPEN_READWRITE
                                | SQLiteDatabase.NO_LOCALIZED_COLLATORS);
            } catch (Exception E) {
                db = null;
            }
        }

        if (db != null) {
            if (c != null) {
                c.close();
                c = null;
            }
            try {
                c = db.rawQuery("SELECT * FROM " + tableName.trim()+" where "+FIELD_TYPENUM+" = "+unitNo, null);
//                KJLoger.debug("=开始从库里面读="+"SELECT * FROM " + tableName.trim()+" where "+FIELD_TYPENUM+" = "+unitNo +"count="+c.getCount());
            } catch (Exception E) {
                c = null;
            }
        }
        if (c != null) {
            if (c.getCount() <= 0) {
                c.close();
                c = null;
            }
        }
        return c;
    }

    public Cursor resourceList(String CurLang, String UnitNo) {
        if (db == null) {
            try {
                db = SQLiteDatabase.openDatabase(CurDBFileName, null,
                        SQLiteDatabase.OPEN_READWRITE
                                | SQLiteDatabase.NO_LOCALIZED_COLLATORS);
            } catch (Exception E) {
                db = null;
            }
        }
        if (db != null) {
            if (c != null) {
                c.close();
                c = null;
            }
            try {
                c = db.rawQuery("SELECT * FROM " + CurLang + " WHERE "
                        + FIELD_TYPENUM + " = " + UnitNo, null);
            } catch (Exception E) {
                c = null;
            }
        }
        if (c != null) {
            if (c.getCount() <= 0) {
                c.close();
                c = null;
            }
        }
        return c;
    }

    /**
     *
     * @Description: 获取数据库整个资源
     * @param CurLang
     * @return
     * @return Cursor
     * @throws
     * @autour wzq
     * @date 2015-7-16 上午11:12:03
     * @update (date)
     */

    public Cursor getAllResoure(String CurLang) {

        if (db == null) {
            try {
                db = SQLiteDatabase.openDatabase(CurDBFileName, null,
                        SQLiteDatabase.OPEN_READWRITE
                                | SQLiteDatabase.NO_LOCALIZED_COLLATORS);
            } catch (Exception E) {
                db = null;
            }
        }
        if (db != null) {
            if (c != null) {
                c.close();
                c = null;
            }
            try {
                c = db.rawQuery("SELECT * FROM " + CurLang , null);
            } catch (Exception E) {
                c = null;
            }
        }
        if (c != null) {
            if (c.getCount() <= 0) {
                c.close();
                c = null;
            }
        }
        return c;
    }

    /**
     * @param CurLang
     * @param UnitNo
     * @return 根据自动讲解编号开始查找数据库
     */

    public Cursor getInfoByAutoNum(String CurLang, int autoNo) {
        if (db == null) {
            try {
                db = SQLiteDatabase.openDatabase(CurDBFileName, null,
                        SQLiteDatabase.OPEN_READWRITE
                                | SQLiteDatabase.NO_LOCALIZED_COLLATORS);
            } catch (Exception E) {
                db = null;
            }
        }
        if (db != null) {
            if (c != null) {
                c.close();
                c = null;
            }
            try {
                c = db.rawQuery("SELECT * FROM " + CurLang + " WHERE "
                        + FIELD_PLAYNUMS + " = " + "'" + autoNo + "'", null);
                ;
            } catch (Exception E) {
                c = null;
            }
        }
        if (c != null) {
            if (c.getCount() <= 0) {
                c.close();
                c = null;
            }
        }
        return c;
    }



    /**
     *
     * @param CurLang
     * @param FileNo
     * @return 根据文件标号查找资源
     */
    public Cursor PlayByFileNum(String CurLang, String FileNo) {
        if (db == null) {
            try {
                db = SQLiteDatabase.openDatabase(CurDBFileName, null,
                        SQLiteDatabase.OPEN_READWRITE
                                | SQLiteDatabase.NO_LOCALIZED_COLLATORS);
            } catch (Exception E) {
                db = null;
            }
        }
        if (db != null) {
            if (c != null) {
                c.close();
                c = null;
            }
            try {
                c = db.rawQuery("SELECT * FROM " + CurLang + " WHERE "
                        + FIELD_FILENO + " = " + "'" + FileNo + "'", null);
            } catch (Exception E) {
                c = null;
            }
        }
        if (c != null) {
            if (c.getCount() <= 0) {
                c.close();
                c = null;
            }
        }
        return c;
    }





    public int checkTableExit(String tableName) {
        int Temp = 0;
        if (db == null) {
            try {
                db = SQLiteDatabase.openDatabase(CurDBFileName, null,
                        SQLiteDatabase.OPEN_READWRITE
                                | SQLiteDatabase.NO_LOCALIZED_COLLATORS);
            } catch (Exception E) {
                db = null;
                Log.d("DB", "null");
            }
        }
        if (db != null) {
            if (c != null) {
                c.close();
                c = null;
            }
            try {
                String sql = "select count(*) as c from sqlite_master where type ='table' and name ='"
                        + tableName.trim() + "' ";
                c = db.rawQuery(sql, null);
            } catch (Exception E) {
                c = null;
                Log.d("c", "null");
            }
            if (c != null && c.getCount() > 0) {
                c.moveToNext();
                Temp = c.getInt(0);
                c.close();
            } else {

            }
            return Temp;

        } else {
            return Temp;
        }
    }


    public Cursor Query(String CurLang, String sqlSentence) {
//        KJLoger.debug(sqlSentence);
        if (db == null) {
            try {
                db = SQLiteDatabase.openDatabase(CurDBFileName, null,
                        SQLiteDatabase.OPEN_READWRITE
                                | SQLiteDatabase.NO_LOCALIZED_COLLATORS);
            } catch (Exception E) {
                db = null;
            }
        }
        if (db != null) {
            if (c != null) {
                c.close();
                c = null;
            }
            try {
                c = db.rawQuery(sqlSentence, null);
            } catch (Exception E) {
                c = null;
            }
        }
        if (c != null) {
            if (c.getCount() <= 0) {
                c.close();
                c = null;
            }
        }
        return c;
    }


}